package com.fc.v2.service;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fc.v2.common.conf.V2Config;
import com.fc.v2.common.file.FileUploadUtils;
import com.fc.v2.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.v2.common.base.BaseService;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.mapper.auto.TSysDatasMapper;
import com.fc.v2.model.auto.TSysDatas;
import com.fc.v2.model.auto.TSysDatasExample;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.util.SnowflakeIdWorker;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;

/**
 * 文件表存储表 TSysDatasService
 * @Title: TSysDatasService.java 
 * @Package com.fc.v2.service 
 * @author fuce_自动生成
 * @email ${email}
 * @date 2021-02-12 15:32:32  
 **/
@Service
public class TSysDatasService implements BaseService<TSysDatas, TSysDatasExample>{
	@Autowired
	private TSysDatasMapper tSysDatasMapper;

	@Autowired
	private V2Config v2Config;


	private static String relativePath;

	private static String absolutePath;

	private static String customPathEnable;

	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<TSysDatas> list(Tablepar tablepar,TSysDatas tSysDatas){
	        TSysDatasExample testExample=new TSysDatasExample();
			//搜索
			if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
	        	testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
	        }else {//大搜索
	        	testExample.createCriteria().andLikeQuery(tSysDatas);
	        }
			//表格排序
			//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        //}else{
	        //	testExample.setOrderByClause("id ASC");
	        //}
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TSysDatas> list= tSysDatasMapper.selectByExample(testExample);
	        PageInfo<TSysDatas> pageInfo = new PageInfo<TSysDatas>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {

			List<String> lista=ConvertUtil.toListStrArray(ids);
			TSysDatasExample example=new TSysDatasExample();
			example.createCriteria().andIdIn(lista);
			return tSysDatasMapper.deleteByExample(example);


	}


	@Override
	public TSysDatas selectByPrimaryKey(String id) {

			return tSysDatasMapper.selectByPrimaryKey(id);

	}


	@Override
	public int updateByPrimaryKeySelective(TSysDatas record) {
		return tSysDatasMapper.updateByPrimaryKeySelective(record);
	}


	@PostConstruct
	public void init(){
		relativePath = v2Config.getRelativePath();
		absolutePath = v2Config.getAbsolutePath();
		customPathEnable = v2Config.getCustomPathEnable();
	}


	/**
	 * 文件上传文件存储到文件表
	 * @param record
	 * @param fileURL
	 * @return 主键
	 * @throws IOException
	 */
	public Map insertSelective(MultipartFile file,Boolean imgCut) throws IOException {
		//文件上传获取文件名字
		String files_name = FileUploadUtils.upload(file,imgCut);
		//相对路径——项目url请求路径
		String relative_filesURL=relativePath + files_name;
		//绝对路径-删除需要得路径
		String absolute_filesURL=null;


		if ("Y".equals(customPathEnable)) {//项目路径
			absolute_filesURL=absolutePath + File.separator + files_name;
		}else {//磁盘路径
			absolute_filesURL=relativePath + File.separator + files_name;
		}

		String fileName=file.getOriginalFilename();
		// 获得文件后缀名称
		String suffixName = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();

		if(StringUtils.isEmpty(suffixName)) {
			//如果没有后缀默认后缀
			suffixName=FileUploadUtils.IMAGE_JPG_EXTENSION;
		}

		TSysDatas record = new TSysDatas();
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
		record.setFilePath(relative_filesURL);
		record.setFileAbsolutePath(absolute_filesURL);
		record.setFileSuffix(suffixName);
		record.setFileName(files_name);
		//上传路径类型
		record.setFileType(customPathEnable);
		if(insertSelective(record)>0)
		{
			//=========不再只返回dataId，而是返回DataId和imgName
			Map<String,String> map=new HashMap<>();
			map.put("data_id", record.getId());
			map.put("imgName",files_name);
			return map;
		}
		return null;
	}
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TSysDatas record) {

		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());


		return tSysDatasMapper.insertSelective(record);
	}


	@Override
	public int updateByExampleSelective(TSysDatas record, TSysDatasExample example) {

		return tSysDatasMapper.updateByExampleSelective(record, example);
	}


	@Override
	public int updateByExample(TSysDatas record, TSysDatasExample example) {

		return tSysDatasMapper.updateByExample(record, example);
	}

	@Override
	public List<TSysDatas> selectByExample(TSysDatasExample example) {

		return tSysDatasMapper.selectByExample(example);
	}


	@Override
	public long countByExample(TSysDatasExample example) {

		return tSysDatasMapper.countByExample(example);
	}


	@Override
	public int deleteByExample(TSysDatasExample example) {

		return tSysDatasMapper.deleteByExample(example);
	}


}
